home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / billboard < prev    next >
Encoding:
Text File  |  2000-05-21  |  6.4 KB  |  187 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # This one's all mine.  Well, its GPL/Artisitic but I"m the author and creator. # I think you need gimp 1.1 or better for this - if  you don't, please let 
  4. # me know
  5.  
  6. # I'm hacking this on top of my sethspin script, so this is doing even more
  7. # stuff it wasn't really designed to do.  Hence if you thought sethspin was
  8. # a bit ugly, look at this one...       
  9.  
  10. # I think it was tigert that suggested this.  It turned out to be less 
  11. # complex than I orginally thought so I figured I'd give it a spin.
  12.  
  13. # Seth Burgess
  14. # <sjburges@gimp.org>
  15.  
  16. use Gimp qw(:auto);
  17. use Gimp::Fu;
  18. use Gimp::Util;
  19.  
  20. # Uncomment below to spew forth much data about whats going on.
  21. #Gimp::set_trace(TRACE_ALL);
  22.  
  23. sub saw {  # a sawtooth function on PI
  24.     ($val) = @_;
  25.     if ($val < 3.14159/2.0) {
  26.         return ($val/3.14159) ;
  27.         }
  28.     elsif ($val < 3.14159) {
  29.         return (-1+$val/3.14159); 
  30.         }
  31.     elsif ($val < 3.14159+3.14159/2.0) {
  32.         return ($val/3.14159) ;
  33.         }
  34.     else {
  35.         return (-1+$val/3.14159); 
  36.         }
  37.     } 
  38.  
  39. sub spin_layer { # the function for actually spinning the layer
  40.     my ($img, $spin, $dest, $numframes, $prp, $blinds) = @_;
  41.     # Now lets spin it!
  42.     $stepsize = 3.14159/$numframes; # in radians 
  43.     for ($i=0; $i<=3.14159; $i+=$stepsize) {
  44.             Gimp->progress_update ($i/3.14159);
  45.         # create a new layer for spinning
  46.         $framelay = ($i < 3.14159/2.0) ?  $spin->copy(1) : $dest->copy(1);
  47.         $img->add_layer($framelay, 0);
  48.         # spin it a step
  49.     # Here I need to make the proper selection, repeatedly if necessary
  50.     $blindheight = $img->height/$blinds;
  51.         for ($j=0; $j<$blinds; $j++) {
  52.             # select a section
  53.             $img->rect_select(0, $j*$blindheight, $img->width, $blindheight, 2, 0, 0.13);
  54.             @x = $img->selection_bounds();
  55.             # x[1],x[2]                  x[3],x[2]
  56.             # x[1],x[4]                  x[3],x[4]
  57.             $floater = $framelay->perspective(1,
  58.         $x[1]+saw($i)*$prp*$framelay->width,$x[2]+$blindheight *sin($i)/2,  
  59.         $x[3]-saw($i)*$prp*$framelay->width,$x[2]+$blindheight *sin($i)/2,
  60.         $x[1]-saw($i)*$prp*$framelay->width,$x[4]-$blindheight *sin($i)/2,  
  61.         $x[3]+saw($i)*$prp*$framelay->width,$x[4]-$blindheight *sin($i)/2);  
  62.  
  63. # Gimp Perspective Functionality has changed.  It used to create a floating 
  64. # selection if there was a selection active already.  Now it only does that
  65. # in interactive - PDB makes it a new layer.  Fine by me, wish the docs had 
  66. # changed though.
  67. #            $floater->floating_sel_anchor;
  68.  
  69.         } # end for ($j=0;...
  70.  
  71.         # I need to create another layer beind this spun one now
  72.         $backlayer = $framelay->layer_copy(0);
  73.         $img->add_layer($backlayer, 1);
  74.         $backlayer->fill(1); # BG-IMAGE-FILL 
  75.     }
  76.     for ($i=0; $i<$numframes; $i++) {
  77.         @all_layers = $img->get_layers();
  78.         $img->set_visible($all_layers[$i],$all_layers[$i+1]);
  79.         $img->merge_visible_layers(0);
  80.         }
  81.     @all_layers = $img->get_layers();
  82.     $destfram = $all_layers[$numframes]->copy(0);
  83.     $img->add_layer($destfram,0);
  84.  
  85.     # clean up my temporary layers    
  86.     $img->remove_layer($all_layers[$numframes]);
  87.     $img->remove_layer($all_layers[$numframes+1]);
  88. }
  89.  
  90. register "billboard",
  91.          "Billboard",
  92.          "Take one image.  Spin it about the multiple axes, and end up with another image.  I made it for easy web buttons, mostly because somebody suggested to me.",
  93.          "Seth Burgess",
  94.          "Seth Burgess <sjburges\@gimp.org>",
  95.          "1.3",
  96.          N_"<Toolbox>/Xtns/Animation/Billboard...",
  97.          "*",
  98.          [
  99.           [PF_DRAWABLE, "source", "What drawable to spin from?"],
  100.           [PF_DRAWABLE, "destination","What drawable to spin to?"],
  101.           [PF_INT8, "frames", "How many frames to use?", 16],
  102.           [PF_COLOR, "background", "What color to use for background if not transparent", [0,0,0]],
  103.           [PF_SLIDER, "perspective", "How much perspective effect to get", 40, [0,255,5]],
  104.           [PF_TOGGLE, "spin_back", "Also spin back?" , 0],
  105.           [PF_TOGGLE, "convert_indexed", "Convert to indexed?", 1],
  106.           [PF_SPINNER, "billboard_slats", "Number of shades", 3, [1,50,1]],
  107.          ],
  108.          [],
  109.          ['gimp-1.1'],
  110.          sub {
  111.    my($src,$dest,$frames,$color,$psp,$spinback,$indexed, $shadenum) =@_;
  112.     $maxwide = ($src->width > $dest->width) ? $src->width : $dest->width;
  113.     $maxhigh = ($src->height > $dest->height) ? $src->height: $dest->height;
  114.     $img = gimp_image_new($maxwide, $maxhigh, RGB);
  115.  
  116.  
  117.     $tmpimglayer = $img->add_new_layer(0,3,1); 
  118.     $img->display_new;
  119.         Gimp->progress_init("Billboard...",-1);
  120.     $oldbackground = gimp_palette_get_background();
  121.     Palette->set_background($color);
  122.     $src->edit_copy();
  123.     $spinlayer = $tmpimglayer->edit_paste(1);
  124.     $spinlayer->floating_sel_to_layer();
  125.  
  126.     $dest->edit_copy();
  127.     $destlayer = $tmpimglayer->edit_paste(1);
  128.     $destlayer->floating_sel_to_layer();
  129.  
  130.     $tmpimglayer->remove_layer;
  131.  
  132.     $spinlayer->resize($maxwide, $maxhigh, $spinlayer->offsets);
  133.     $destlayer->resize($maxwide, $maxhigh, $destlayer->offsets);
  134.     # work around for PF_SLIDER when < 1
  135.     $psp = $psp/255.0;
  136.  
  137.     # need an even number of frames for spinback
  138.     if ($frames%2 && $spinback) {
  139.         $frames++;
  140.         gimp_message("An even number of frames is needed for spin back.\nAdjusted frames up to $frames");
  141.         }
  142.  
  143.     spin_layer($img, $spinlayer, $destlayer, $spinback ? $frames/2 : $frames-1, $psp, $shadenum);
  144.      $img->set_visible($img->add_new_layer(1),($img->get_layers)[0]); 
  145.      $img->merge_visible_layers(0);
  146.     
  147.     if ($spinback) { 
  148.         @layerlist = $img->get_layers();
  149.         $img->add_layer($layerlist[$frames/2]->copy(0),0);
  150.         @layerlist = $img->get_layers();
  151.         spin_layer($img, $layerlist[1], $layerlist[0], $frames/2, $psp, $shadenum);
  152.         $img->remove_layer(($img->get_layers)[0]);
  153.         }    
  154.     
  155.     # unhide and name layers
  156.     @all_layers = $img->get_layers;
  157.     $img->set_visible(@all_layers);
  158.     for ($i=1; $i<=$frames ; $i++) {
  159.         $all_layers[$i-1]->set_name("Spin Layer $i (50ms)");
  160.         }
  161.     $all_layers[$frames-1]->set_name("Spin Layer SRC (250ms)");  
  162.     
  163.     if ($spinback) {
  164.         $all_layers[$frames/2-1]->set_name("Spin Layer DEST (250ms)"); 
  165.         }
  166.     else { $all_layers[0]->set_name("Spin Layer DEST (250ms)")}
  167.     
  168.  
  169.     # indexed conversion wants a display for some reason
  170.     if ($indexed) { 
  171.            $img->convert_indexed(1,            # dither type = fs
  172.                                  MAKE_PALETTE, # palette type
  173.                                  255,          # number of colors 
  174.                                  0,            # don't dither transparency
  175.                                  1,            # (ignored)
  176.                                  "Custom"      # custom palette name 
  177.                                  );
  178.            } 
  179.  
  180.     Palette->set_background($oldbackground);
  181.     gimp_displays_flush();
  182.     return();
  183. };
  184.  
  185. exit main;
  186.  
  187.